package com.jeepig.web.controller.monitor;

import com.jeepig.common.core.controller.BaseController;
import com.jeepig.common.core.domain.AjaxResult;
import com.jeepig.common.utils.DateUtils;
import com.jeepig.system.domain.SysLogininfor;
import com.jeepig.system.service.ISysLogininforService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @ClassName IndexController
 * @Description 首页
 * @Author guoxb
 * @Date 2022/12/25
 * @Version 1.0
 **/
@RestController
@RequestMapping("/monitor/index")
public class IndexController extends BaseController {
    @Autowired
    private ISysLogininforService logininforService;
    /**
     *
     * 统计订单个数，金额
     * 最近七天的订单数量及金额
     * @return com.jeepig.common.core.domain.AjaxResult
     * @date 2022/12/25 14:10
     */
    @RequestMapping("/home")
    public AjaxResult index(){
        //最近七天的
        int[] counts = new int[7];
        Calendar calendar = new GregorianCalendar();
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -7);
        Date dayStart = calendar.getTime();
        Date[] day = getLast7day(dayStart);
        String[] dayStr = new String[day.length];
        for(int i = 0;i < day.length; i++){
            dayStr[i] = DateUtils.parseDateToStr("yyyy-MM-dd",day[i]);
            counts[i] = logininforService.selectTotal(dayStr[i], dayStr[i]);
        }
        Map result = new HashMap();
        result.put("counts",counts);
        result.put("days",dayStr);
        return AjaxResult.success(result);
    }

    public Date[] getLast7day(Date today){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(today);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Date[] day = new Date[7];
        for(int i = 0;i < 7; i++){
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            day[i]= calendar.getTime();
        }
        return day;
    }
}
